﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Domain.Common;
using Domain.UserManagement;
using Domain.UserManagement.Entities;
using Infrastructure.Common.Data;
using System.Linq.Expressions;

namespace Infrastructure.Data.Security
{
    public class UserRepository : Repository<User>, IUserRepository
    {
        public UserRepository(IContext context)
            : base(context)
        { }

        public override User Add(User item)
        {
            return this.Context.Users.Add(item);
        }

        public override void Update(User item)
        {
            throw new NotImplementedException();
        }

        public override void Delete(User item)
        {
            this.Context.Users.Remove(item);
        }

        public override IEnumerable<User> Get(params Expression<Func<User, bool>>[] predicates)
        {
            var q = from s in this.Context.Users select s;

            if (predicates != null)
            {
                foreach (var predicate in predicates)
                {
                    q = q.Where(predicate);
                }
            }

            return q.ToArray();
        }

        public User Get(LoginAttempt attempt)
        {
            throw new NotImplementedException();
        }

        public User Get(string userName)
        {
            var q = from s in this.Context.Users select s;

            foreach (var user in q)
            {
                if(user.UserName == userName)
                {
                    return user;
                }
            }

            return null;          
        }

        public IUnitOfWork UnitOfWork
        {
            get { throw new NotImplementedException(); }
        }
    }
}
